import os
import sys
import urllib
import hashlib
import platform

depjars = [
	'lib/jcommon-1.0.10.jar',
	'lib/jfreechart-1.0.6.jar',
	'lib/jfreechart-1.0.6-swt.jar',
	'lib/swtgraphics2d.jar',
]

swt_jars = {
	('linux2', 32) : 'gtk32',
	('linux2', 64) : 'gtk64',
	('darwin', 32) : 'osx32',
	('darwin', 64) : 'osx64',
	('win32',  32) : 'win32',
	('win32',  64) : 'win64',
}

Import('java_env', 'AddResources')

jar_name = 'soar-debugger.jar'
shortcut_name = 'SoarJavaDebugger.jar'
swt = 'lib/swt/%s/swt.jar' % swt_jars[(sys.platform, 64 if '64' in platform.machine() else 32)]
api_jar = java_env.File('$OUT_DIR/java/soar-debugger-api.jar')

clone = java_env.Clone()

# Due to VariantDir weirdness, I have to distinguish between the libraries
# that can be found in their original directories and libraries that can
# be found in the build and output directories. Hence libs1 and libs2.
libs1 = [clone.File(j) for j in depjars]
libs1.append(clone.File(swt))
libs2 = [java_env['SML'], api_jar]

cp = clone['SEP'].join([l.srcnode().abspath for l in libs1] + \
              [l.abspath for l in libs2])

clone['JAVACLASSPATH'] = cp

classes = clone.Java(target = 'classes', source='src')

clone.Depends(classes, libs1 + libs2)
temp = clone.Jar(
	target   = 'temp.jar',
	source   = ['classes', 'manifest1'],
	JARCHDIR = clone.Dir('classes')
)

jar = clone.Command(
	target = jar_name,
	source = [temp, clone.Dir('resources')],
	action = AddResources
)

shortcut_jar = clone.Jar(target = shortcut_name, source = 'manifest2')
clone.Depends(shortcut_jar, jar)

install = clone.Install('$OUT_DIR/java', jar + libs1)
install_shortcut = clone.Install('$OUT_DIR', shortcut_jar)

clone.Alias('debugger', install + install_shortcut)
